/**
 * Created by Jay on 2016/6/20.
 */
function  get_page_no(){
    var page=set_page_no();
    page=page||{};
    //console.log(page.pageNo);
    page.pageNo=page.pageNo||"null";
    return  page.pageNo;
}//pageNo

function  get_page_size(){
    var page=set_page_no();
    page=page||{};
    //console.log(page.pageSize);
    page.pageSize=page.pageSize||"null";
    return  page.pageSize;
}//pageSize

function set_page_no(t_page){
    //page.pototype.pageSize=this.page.pageSize;
    //page.pototype.pageNo=this.pageNo;
    //page.prototype.get_page_size= function () {
    //     return   page.pototype.pageSize
    //}
    //page.prototype.get_page_no= function () {
    //    return    page.pototype.pageNo
    //}
    page=t_page;
    //console.log(page);
}//保存page数据




/*--栏目配置-栏目管理--*/
//列表
function columnManage(pageNo,id){
    //给栏目表录入数据
    if(!pageNo){pageNo=1}		//分页页数
    if(!id){id=0}
    mccmsa.ajax({
        url:httpUrl+"column/getColumnsSubList/"+id,
        method:"GET",
        success:function(json){
            Handlebars.registerHelper("addOne",function(index,options){
                return parseInt(index)+1;
            });
            mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
                templateId: "navMain",
                container: $("#pathContainer")
            });

            mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
                templateId: "getColumnTableData",
                container: $("#ColumnTableData"),
                data:json
            });
            pageHtml(json);		//生成分页
            $("#columnManage,.zTreeDemoBackground").show();
            $("#onClickDataTable").hide();

            //
            //$(".zTreeDemoBackground").append(treeDemo)

            var scrollbarWidth = $('.table-maxHeight')[0].offsetWidth - $('.table-maxHeight')[0].scrollWidth;
            $("table:eq(0)").css("paddingRight",scrollbarWidth);    //获取滚动条宽度并赋值到表头padding-right，对齐
        }
    })
}



//编辑
function columnEdit(obj,treeNode){
    var id,parentName;
    if(!obj){//tree 时argument...
        id=treeNode.id;
        parentName=treeNode.getParentNode().description;
    }else{//是对象时。。。。
        id=$(obj).parents("tr").attr("columnid");
        //console.log(id);
        //parentName=$.fn.zTree.getZTreeObj("treeDemo").getNodeByParam("id", id, null).getParentNode().description;//这个有什么用啊。。。。。。。。。。。。
    }
    mccmsa.ajax({
        url:httpUrl+"column/getColumnTemplate",
        method:"GET",
        success:function(json){
            var html = "";
            for(var i=0;i<json.result.length;i++){
                html+='<option value="'+json.result[i].id+'">'+json.result[i].name+'</option>'
            }
            mccmsa.ajax({
                url:httpUrl+"column/getColumnById/"+id,
                method:"GET",
                success:function(json){
                    mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
                        templateId: "navEdit",
                        container: $("#pathContainer")
                    });

                    mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
                        templateId: "getEditDataColumn",
                        container: $("#onClickDataTable"),
                        data:json
                    });
                    $("#postColumnTemplate").html(html);

                    $("#columnManage,.zTreeDemoBackground").hide();
                    $("#onClickDataTable").show();

                    $("#isShowInNav option[value='"+json.result.isShowInNav+"']").attr("selected","selected");
                    $("#columnState option[value='"+json.result.state+"']").attr("selected","selected");
                    $("#postColumnTemplate option[value='"+json.result.template+"']").attr("selected","selected");

                    //$("#sureParents").val(parentName);//这个也是。。。什么啊。。。。。。

                    $("#parentsZTree .modal-body .modal-treeMenu").html(treeDemo);

                }
            })
        }
    })
}
function columnPostEdit(){
    mccmsa.ajax({
        url:httpUrl+"column/editColumn/",
        method:"POST",
        data:{
            "id":$("#postColumnId").attr("columnId"),
            "name":$("#postColumnName").val(),
            "shortName":$("#postColumnShortName").val(),
            "isShowInNav":$("#isShowInNav option:selected").val(),
            "columnKey":$("#postColumnColumnKey").val(),
            "serial":$("#postColumnSerial").val(),
            "template":$("#postColumnTemplate option:selected").val(),
            "link":$("#postColumnLink").val(),
            //"parent":$.fn.zTree.getZTreeObj("treeDemo").getNodeByParam("description", $("#sureParents").val(), null).id,
            "state":$("#columnState option:selected").val()
        },
        success:function(json){
            columnManage();
        }
    })
}
//新增数据
function columnNew(){
    mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
        templateId: "navNew",
        container: $("#pathContainer")
    });

    mccmsa.getHtmlFromLocalTemplate({   //将数据打包到模版丢进容器
        templateId: "getNewDataColumn",
        container: $("#onClickDataTable")
    });
    $("#parentsZTree .modal-body .modal-treeMenu").html(treeDemo);
    $("#columnManage,.zTreeDemoBackground").hide();
    $("#onClickDataTable").show();

    mccmsa.ajax({
        url:httpUrl+"column/getColumnTemplate",
        method:"GET",
        success:function(json){
            var html = "";
            for(var i=0;i<json.result.length;i++){
                html+='<option value="'+json.result[i].id+'">'+json.result[i].name+'</option>'
            }
            $("#postColumnTemplate").html(html);
        }
    })
}
function columnPostNew(){
    mccmsa.ajax({
        url:httpUrl+"column/newColumn/",
        method:"POST",
        data:{
            "id":$("#postColumnId").attr("columnId"),
            "name":$("#postColumnName").val(),
            "shortName":$("#postColumnShortName").val(),
            "isShowInNav":$("#isShowInNav option:selected").val(),
            "columnKey":$("#postColumnColumnKey").val(),
            "serial":$("#postColumnSerial").val(),
            "template":$("#postColumnTemplate option:selected").val(),
            "link":$("#postColumnLink").val(),
            "parent":$.fn.zTree.getZTreeObj("treeDemo").getNodeByParam("description", $("#sureParents").val(), null).id,
            "state":$("#columnState option:selected").val()
        },
        success:function(json){
            columnManage();
        }
    })
}
//删除数据
function postDelColumnData(obj){

    mccmsa.ajax({
        url:httpUrl+"column/deleteColumn/"+$("#myModal").attr("columnId"),
        method:"POST",
        data:{
            "_method":"delete"
        },
        success:function(json){
            $("#myModal .close").click();
            columnManage();
        }
    })
}
function columnDel(obj){
    $("#myModal").attr("columnId",$(obj).parents("tr").attr("columnid"))
}

/*--栏目配置-内容管理--*/

/*系统配置>权限管理 的tree的 按钮*/
$(function(){
    /*sea.js 的引入--加快速度的说*/
//    seajs.config({
//        base: "sea-modules",
//        alias: {
//             "$": "sea-modules/jquery.min.js",//jq
//             "bbq":"sea-modules/jquery.ba-bbq.js",//bbq
//             "valid":"libs/sea-modules/Validform_v5.3.2_min.js",//validate
//             "page":"libs/page.js",//page
//             "z-tree-core":"libs/zTree/js/jquery.ztree.core-3.5.js",//ztree
//             "z-tree-e":"libs/zTree/js/jquery.ztree.excheck-3.5.js"//ztree
//        }
//    })
//
//// 加载入口模块
//    seajs.use("../static/hello/src/main");
//    seajs.use("../static/hello/src/main");
//    //logout!!退出
    $(".logout").on("click",function(){
        window.location="logout";
    })
//zTree树节点点击时显示编辑，删除
    $("#mainContainer").on("click","#permissionManage .treeTip a",function(event){
        $(".treeFunction").remove();
        var html = "<div class='treeFunction'><span class='treeEdit' onClick='getEditPermissionData(this)'>编辑</span><span class='treeDel' data-toggle='modal' href='#myModal' onclick='modalUser(this)'>删除</span></div>";
        $(this).append(html)
    });
//权限，当权限类型选为功能时，隐藏父级权限选择
    $("#mainContainer").on("change","#onClickDataTable #select",function(event){
        if($(this).val()==2){
            $("#permFirstTr").hide();
        }else{
            $("#permFirstTr").show();
        }
    });
})

/*enter 搜索*/
function search_on_enter(opt){
    opt.this=opt.this||undefined;
    var dtd = $.Deferred();
    opt.this.keydown(function (e){
        if(e.which == "13"){
             alert();
            dtd.resolve(); // 改变deferred对象的执行状态
        }
    })
    return dtd;
}


var t_p={};

t_p.ajax=function(opt){
    opt.url=opt.url||undefined;
    opt.method=opt.method||undefined;
    opt.data=opt.data||undefined;
    opt.temp=opt.temp||undefined;
    opt.contain=opt.contain||undefined;
    for(var i in opt){
        if(opt[i]==undefined){
             console.log(this);
             console.log("少了参数");
             return;
        }
    }
    $.ajax({
        url:opt.url,
        method:opt.method,
        data:opt.data
    })
    /*请求失败*/
    .fail(function(data){
        console.log("失败了");
        console.log(data);
    })
    /*请求成功*/
    /*判断业务上的成功或失败*/
    .done(function(data){
        var dtd = $.Deferred();
        if(data.status==200){
            dtd.resolve(); // 改变deferred对象的执行状态
        }else{
            dtd.reject();
        }
        return dtd.promise();
    })
    /*业务上成功 status==200*/
    .done(function(data){
        if(data.result.resultList==undefined){
            data.result.resultList=data.result;
        }
        //console.log(data);
        /*序号*/
        set_seqsum_conver.call(this,data);
        /*生成分页*/
        create_page_cover.apply(this,[data,opt]);
        /*将数据打包到模版丢进容器*/
        template.call(this,opt)
    })
    /*业务上失败 status！==200*/
    .fail(function(){

    })
}
/*序号*/
function set_seqsum_conver(data){
    set_seqsum(
        {
            pageNo:data.result.pageNo,
            pageSize:data.result.pageSize
        }
    );
}
/*生成分页*/
function create_page_cover(data,opt){
    create_page({
        url:opt.url,
        method:opt.method,
        data:data,
        temp:opt.temp,
        contain:opt.contain,
        prePageSize:data.result.pageSize,
        prepageNo:data.result.pageNo
    })
    var return_data=1*data.result.pageNo;//返回的首位数
    sessionStorage.setItem("pageNo",data.result.pageNo);
    sessionStorage.setItem("pageSize",data.result.pageSize);
    sessionStorage.setItem("return_data",return_data);
    var page=get_page_size();
}
/*将数据打包到模版丢进容器*/
function template(opt){
    mccmsa.getHtmlFromLocalTemplate({
        templateId:opt.temp,
        container:opt.contain,
        data: data,
        callback:function(){
        }
    })
}


